﻿Public Enum Output
    WebSite = 1
    ControlLibrary = 2
End Enum

Public Class WebSite

    Private _batchToWrite As String = String.Empty
    Private _tempDirectory As String = String.Empty


    Private _shouldMerge As Boolean
    Public Property ShouldMerge() As Boolean
        Get
            Return _shouldMerge
        End Get
        Set(ByVal value As Boolean)
            _shouldMerge = value
        End Set
    End Property



    Private _originalPath As String
    Public Property OriginalPath() As String
        Get
            Return _originalPath
        End Get
        Set(ByVal value As String)
            _originalPath = value
        End Set
    End Property

    Private _iisPath As String
    Public Property IisPath() As String
        Get
            Return _iisPath
        End Get
        Set(ByVal value As String)
            _iisPath = value
        End Set
    End Property

    Private _outputType As Output
    Public Property OutputType() As Output
        Get
            Return _outputType
        End Get
        Set(ByVal value As Output)
            _outputType = value
        End Set
    End Property

    Private _outputPath As String
    Public Property OutputPath() As String
        Get
            Return _outputPath
        End Get
        Set(ByVal value As String)
            _outputPath = value
        End Set
    End Property

    Private _outputName As String
    Public Property OutputName() As String
        Get
            Return _outputName
        End Get
        Set(ByVal value As String)
            _outputName = value
        End Set
    End Property

    Private _fixedNames As Boolean
    Public Property FixedNames() As Boolean
        Get
            Return _fixedNames
        End Get
        Set(ByVal value As Boolean)
            _fixedNames = value
        End Set
    End Property

    Private _partialBuild As Boolean
    Public Property PartialBuild() As Boolean
        Get
            Return _partialBuild
        End Get
        Set(ByVal value As Boolean)
            _partialBuild = value
        End Set
    End Property

    Private _writeBatch As Boolean
    Public Property WriteBatch() As Boolean
        Get
            Return _writeBatch
        End Get
        Set(ByVal value As Boolean)
            _writeBatch = value
        End Set
    End Property

    Public Sub Compile()

        If Not String.IsNullOrEmpty(Me.OutputPath) Then
            Me._tempDirectory = Me.OutputPath & "\Temp"
        End If

        Select Case Me.OutputType
            Case Output.WebSite
                Publish()
                If (Me.ShouldMerge) Then
                    Merge()
                End If
                If (Me.WriteBatch) Then
                    WriteBatchFile()
                End If
            Case Output.ControlLibrary
                Me.PartialBuild = False
                Me.FixedNames = True
                Publish()
                Merge()
                WriteBatchFile()
        End Select

    End Sub

    Private Sub Publish()
        Dim dir As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.System))

        dir = dir.Parent

        Dim cmd As String = dir.FullName & "\Microsoft.Net\Framework\v2.0.50727\aspnet_compiler.exe"

        If Not String.IsNullOrEmpty(Me.OriginalPath) AndAlso Not String.IsNullOrEmpty(Me.OutputPath) Then
            Dim str As New System.Text.StringBuilder
            If Not String.IsNullOrEmpty(Me.IisPath) Then
                str.Append(" -m ")
                str.Append(Me.IisPath)
                str.Append(" ")
            Else
                str.Append(" -v / -p """)
                str.Append(Me.OriginalPath)
                str.Append(""" ")
            End If

            str.Append("""" & Me._tempDirectory & """")
            str.Append(" ")

            If Me.PartialBuild Then
                str.Append(" -u ")
            End If

            If Me.FixedNames Then
                str.Append(" -fixednames ")
            End If

            str.Append(" -f -c")

            Dim myProcess As New System.Diagnostics.Process

            myProcess.StartInfo.Arguments = str.ToString
            myProcess.StartInfo.FileName = cmd
            myProcess.StartInfo.WorkingDirectory = dir.ToString
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

            Try
                myProcess.Start()
                myProcess.WaitForExit()
            Catch ex As Exception
                Throw
            Finally
                myProcess.Dispose()
            End Try

            _batchToWrite += """" + cmd + """ "
            _batchToWrite += str.ToString() + Environment.NewLine
        End If
    End Sub

    Private Sub Merge()
        'Obtenir le repertoire de windows
        Dim dir As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))

        Dim cmd As String = dir.FullName & "\Microsoft\ILMerge\ILMerge.exe"
        Dim args As String = " /ndebug"

        'Chercher dans le repertoire de sortie l'ensemble des assemblys sorties...
        Dim outputDir As String = String.Empty
        If (Not String.IsNullOrEmpty(Me.OriginalPath)) Then
            outputDir = Me._tempDirectory + "\bin\"
        End If

        Dim files As String() = IO.Directory.GetFiles(outputDir, "*.ascx.*.dll")

        If (files.Length > 0) Then
            args += " /out:""" + Me.OutputPath + "\" + Me.OutputName + """"
        End If

        For Each file As String In files
            args += " """ + file + """"
        Next

        Dim myProcess As New System.Diagnostics.Process

        myProcess.StartInfo.Arguments = args
        myProcess.StartInfo.FileName = cmd
        myProcess.StartInfo.WorkingDirectory = dir.ToString
        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal

        Try
            myProcess.Start()
            myProcess.WaitForExit()
        Catch ex As Exception
            Throw
        Finally
            myProcess.Dispose()
        End Try


        _batchToWrite += """" + cmd + """ "
        _batchToWrite += args

    End Sub

    Private Sub WriteBatchFile()
        If Not String.IsNullOrEmpty(_batchToWrite) Then

            Dim file As IO.StreamWriter = Nothing

            If Not String.IsNullOrEmpty(_batchToWrite) Then
                Try
                    file = New IO.StreamWriter(Me.OutputPath + "\" + "Compile.bat")
                    file.WriteLine(_batchToWrite)
                Catch ex As Exception
                Finally
                    If file IsNot Nothing Then
                        file.Dispose()
                    End If
                End Try
            End If
        End If

    End Sub

End Class
